package cn.itcast.manage;

import cn.itcast.bean.UmPassword;
import cn.itcast.bean.User;
import cn.itcast.dao.AddServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String unum = req.getParameter("unum");
        String password = req.getParameter("password");
        resp.setContentType("text/html;charset=UTF-8");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
           connection = DriverManager.getConnection("jdbc:mysql://localhost/db5", "root", "");
            preparedStatement = connection.prepareStatement("select * from t_nump where uid=? and password=?");
            preparedStatement.setString(1,unum);
            preparedStatement.setString(2,password);
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()){
                resp.getWriter().write("用户名或密码错误,请重新输入");
                resp.setHeader("refresh","2;url=/login.jsp");

            }else {
                resp.sendRedirect("/index.jsp");
                req.getSession().setAttribute("unum",unum);}

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }
      /*  UmPassword umPassword = new UmPassword();
        umPassword.setUnum(unum);
        umPassword.setPassword(password);
        AddServlet a = new AddServlet();
        List<UmPassword> umPasswords = a.find();
        for (UmPassword umPassword1 : umPasswords) {
            System.out.println(umPassword1.getUnum());
            System.out.println(unum);
            if(unum.equals(umPassword1.getUnum() + "")&&umPassword1.getPassword().equals(password)){
                resp.getWriter().write("登入成功");
                resp.setHeader("refresh","2;url=/index.jsp");
                req.getSession().setAttribute("unum",unum);
                return;
            }

        }*/







    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp);
           }
}
